<template>
  <div class="subscribe_page">
    <div class="input_box dis_flex just_center">
      <searchInput
        class="search_input"
        type="text"
        v-model="value"
        placeholder="搜索咨询内容"
        @input="handleSearch"
      ></searchInput>
      <div class="dis_flex" @click="isShowFilter = true">
        <div class="filtrate_txt">筛选</div>
        <img
          src="@/assets/images/evaluate/filtrate_icon.png"
          alt=""
          class="filtrate_img"
        />
      </div>
    </div>
    <div class="record_list" @scroll="handleScroll">
      <div
        v-for="(item, index) in consultRecordList"
        :key="index"
        class="item_record"
        @touchstart="startLongPress(item)"
        @touchend="endLongPress"
        @touchcancel="endLongPress"
      >
        <div class="dis_flex width100">
          <img :src="joinUrlImg(item.consult_avatar)" alt="" class="item_img" />
          <div class="dis_flex just_between width100">
            <div class="">
              <div class="dis_flex">
                <div class="item_name">{{ item.consult_name }}</div>
                <img
                  src="@/assets/images/common/female.png"
                  alt=""
                  class="sex_img"
                  v-if="item.sex == 1"
                />
                <img
                  src="@/assets/images/common/man.png"
                  alt=""
                  class="sex_img"
                  v-else
                />
                <div class="item_age">{{ item.consult_age }}岁</div>
              </div>
              <div class="item_person">
                由{{ item.type == 1 ? "用户本人" : "他人" }}预约
              </div>
            </div>
            <div class="item_state">{{ getState(item) }}</div>
            <!-- <div>已结束</div>
          <div>已取消</div> -->
          </div>
        </div>
        <div class="item_line01"></div>
        <div class="dis_flex">
          <div class="item_txt">预约时段：</div>
          <div class="dis_flex">
            <div class="item_time time_01">
              {{ item.reservation_time.split(" ")[0] }}
            </div>
            <div class="item_time time_02">
              {{ item.reservation_time.split(" ")[1] }}
            </div>
          </div>
        </div>
        <div class="dis_flex mar_top14">
          <div class="item_txt">问题类型：</div>
          <div class="item_desc">{{ item.question_name }}</div>
        </div>
        <div class="dis_flex mar_top14">
          <div class="item_txt">提交日期：</div>
          <div class="item_desc">{{ timestampToUTCDate(item.time) }}</div>
        </div>
        <div class="dis_flex mar_top14">
          <div class="item_txt">预约地点：</div>
          <div class="item_desc">{{ item.address }}</div>
        </div>
        <div class="item_line02"></div>
        <div class="dis_flex just_between">
          <div
            class="item_operation dis_flex"
            @click="jumpPage(item.is_end, `/appointmentPreTask?id=${item.id}`)"
          >
            <img
              src="@/assets/images/consult/task_icon.png"
              alt=""
              class="operation_img"
            />
            <span class="operation_txt">前置任务</span>
          </div>
          <div
            class="item_operation dis_flex"
            @click="jumpPage(item.is_end, `/cancelAppointment?id=${item.id}`)"
          >
            <img
              src="@/assets/images/consult/close_icon.png"
              alt=""
              class="operation_img"
            />
            <span class="operation_txt">取消预约</span>
          </div>
          <div
            class="item_operation dis_flex"
            @click="
              jumpPage(
                item.is_end,
                `/appointmentDetails?id=${item.id}`,
                'details'
              )
            "
          >
            <img
              src="@/assets/images/consult/search_icon.png"
              alt=""
              class="operation_img"
            />
            <span class="operation_txt">查看详情</span>
          </div>
        </div>
      </div>
      <div class="no_data_txt">-- 无更多咨询记录了--</div>
    </div>

    <!-- 筛选弹框 -->
    <filterPop
      :isShowPop="isShowFilter"
      @closeFilterPop="closeFilterPop"
      :type="4"
    />
    <!-- 操作弹框 -->
    <recordOperation
      :isShowOperationPop="isShowOperationPop"
      :type="4"
      @buttonOperation="buttonOperation"
    />
  </div>
</template>

<script>
import { timestampToUTCDate, cutOutHMS } from "@/utils/time";
import { joinUrlImg } from "@/utils/setUrlImg";
import { queryOffConRecord, delOffConRecord } from "@/api/consultation/record";
import filterPop from "@/views/nav/evaluate/evaluationRecord/components/filterPop.vue";
import recordOperation from "@/views/nav/evaluate/evaluationRecord/components/recordOperation.vue";
import searchInput from "@/components/searchInput/index.vue";
export default {
  data() {
    return {
      value: "",
      longPressTimer: null, // 长按定时器
      isShowFilter: false,
      isShowOperationPop: false,
      consultRecordList: [],
      total: null,
      pageSize: 5,
      page: 1,
      filterInfo: {},
      currentSubscribeInfo: {},
    };
  },
  components: {
    searchInput,
    filterPop,
    recordOperation,
  },
  created() {
    this.gteOffConRecord();
  },
  methods: {
    timestampToUTCDate,
    cutOutHMS,
    joinUrlImg,
    handleScroll(e) {
      const container = e.target;
      const containerHeight = container.clientHeight;
      const contentHeight = container.scrollHeight;
      const scrollTop = container.scrollTop;
      if (scrollTop + containerHeight > contentHeight) {
        if (this.total > this.consultRecordList.length) {
          this.page += 1;
          this.gteOffConRecord(false);
        }
      }
    },
    getState(item) {
      if (item.is_end == 1) {
        switch (item.situation) {
          case 2:
            return "已取消";
          case 3:
            return "正常";
          case 4:
            return "爽约";
        }
      } else if (item.is_end == 0) {
        return "未开始";
      }
    },
    //查询预约记录
    async gteOffConRecord(shouldReset = false) {
      // 如果需要重置，清空记录列表
      if (shouldReset) {
        this.consultRecordList = [];
        this.page = 1; // 重置页码
      }
      let par = {
        pageSize: this.pageSize,
        page: this.page,
        question: this.value,
        is_end: this.filterInfo.is_end,
        start_time: this.filterInfo.start_time,
        end_time: this.filterInfo.end_time,
        question_id: this.filterInfo.question_id,
        is_pre_task: this.filterInfo.is_pre_task,
      };
      let res = await queryOffConRecord(par);
      if (res.code == 200) {
        this.consultRecordList.push(...res.data.data);
        this.total = res.data.total;
      }
    },

    jumpPage(isEnd, path, val) {
      if (isEnd == 0) {
        this.$router.push({ path: path });
      }
      if (isEnd == 1 && val == "details") {
        this.$router.push({ path: path });
      }
    },
    closeFilterPop(val) {
      this.isShowFilter = false;
      this.filterInfo = val;
      this.gteOffConRecord(true);
    },
    buttonOperation(val) {
      if (val == "detail") {
        this.$router.push(
          `/appointmentDetails?id=${this.currentSubscribeInfo.id}`
        );
      } else if (val == "del") {
        this.isShowOperationPop = false;
        this.$popup({
          title: "提示",
          message: `<div>确定要删除本条记录吗？</div>`,
          confirmOperate: async () => {
            let res = await delOffConRecord({
              id: this.currentSubscribeInfo.id,
            });
            if (res.code == 200) {
              this.$toast.success("删除成功!");
              this.gteOffConRecord(true);
            }
          },
        });
      }
    },
    startLongPress(item) {
      this.currentSubscribeInfo = item;
      this.longPressTimer = setTimeout(() => {
        this.isShowOperationPop = true;
      }, 500);
    },
    endLongPress() {
      clearTimeout(this.longPressTimer);
    },
    handleSearch(value) {
      this.value = value;
      if (this.searchTimer) {
        clearTimeout(this.searchTimer);
      }
      this.searchTimer = setTimeout(() => {
        this.page = 1;
        this.consultRecordList = [];
        this.gteOffConRecord();
      }, 500);
    },
    openPop() {
      this.$emit("openPop");
    },
  },
};
</script>

<style lang="less" scoped>
.dis_flex {
  display: flex;
  align-items: center;
}
.just_center {
  justify-content: center;
}
.just_between {
  justify-content: space-between;
}
.felx1 {
  flex: 1;
}
.width100 {
  width: 100%;
}
.mar_top14 {
  margin-top: 14px;
}

.input_box {
  width: 100%;
  height: 50px;
  background: #ffffff;
  border-bottom: 1px solid #dddddd;
}
.search_input {
  width: 270px !important;
  height: 36.67px;
  margin: 0 10px 0 0;
  background: #f4f6fa;
}
.filtrate_txt {
  color: #333333;
  font-size: 14px;
  font-size: 14px;
}
.filtrate_img {
  width: 13.33px;
  height: 13.33px;
  margin-left: 5.67px;
}
.record_list {
  height: calc(100vh - 160px);
  overflow: hidden;
  overflow-y: scroll;
}
.record_list::-webkit-scrollbar {
  display: none;
}
.item_record {
  padding: 13.33px 10px 20px 10px;
  background: #fff;
  border-radius: 6.67px;
  margin-bottom: 10px;
}
.item_img {
  width: 53.33px;
  height: 53.33px;
  margin-right: 10px;
}
.item_name,
.item_age {
  font-weight: bold;
  font-size: 16px;
  line-height: 16px;
  color: #333333;
}
.sex_img {
  width: 19.33px;
  height: 19.33px;
  margin: 0 4.67px;
}
.item_person {
  font-size: 12.67px;
  line-height: 12.67px;
  margin-top: 13px;
  color: #aaaaaa;
}
.item_state {
  line-height: 14px;
  font-size: 14px;
  color: #00aeff;
}
.item_line01 {
  border-bottom: 0.33px solid #dddddd;
  margin: 10px 0;
}
.item_line02 {
  border-bottom: 0.33px solid #dddddd;
  margin: 22px 0 14px;
}
.item_txt {
  text-align: left;
  font-size: 14px;
  line-height: 14px;
  color: #666666;
  margin-right: 13.67px;
}
.item_time {
  color: #ffffff;
  font-size: 13.33px;
  padding: 6px 10px;
  border-radius: 11.67px;
}
.time_01 {
  background: linear-gradient(0deg, #ffffff -20%, rgba(255, 255, 255, 0) 20%),
    #29cca6;
  margin-right: 4px;
}
.time_02 {
  background: linear-gradient(0deg, #ffffff -20%, rgba(255, 255, 255, 0) 20%),
    #ff9500;
}
.item_desc {
  color: #333333;
  font-size: 14px;
  line-height: 14px;
}
.operation_img {
  width: 15.33px;
  height: 15.33px;
  margin-right: 10px;
}
.operation_txt {
  color: #333333;
  font-size: 14px;
}
.no_data_txt {
  font-size: 14px;
  margin: 32px auto;
  text-align: center;
  color: #777777;
}
</style>
